package com.ruoyi.nmy.text;
import java.util.*;
public class NumberToChinese {

        private static final String[] DIGITS = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
        private static final String[] UNITS = {"", "拾", "佰", "仟"};
        private static final String[] BIG_UNITS = {"", "万", "亿"};

        public static String numberToChinese(double num) {
            if (num < 0) {
                return "负" + numberToChinese(-num);
            }

            String strNum = String.format("%.2f", num);
            String[] parts = strNum.split("\\.");
            String integerPart = parts[0];
            String decimalPart = parts[1];

            String integerChinese = convertInteger(integerPart);
            String decimalChinese = convertDecimal(decimalPart);

            return integerChinese + "元" + decimalChinese;
        }

        private static String convertInteger(String integerStr) {
            int length = integerStr.length();
            if (length > 12) {
                throw new IllegalArgumentException("数字太大，无法转换");
            }

            StringBuilder result = new StringBuilder();
            for (int i = 0; i < length; i++) {
                int digit = integerStr.charAt(i) - '0';
                int unitIndex = (length - i - 1) % 4;
                int bigUnitIndex = (length - i - 1) / 4;

                if (digit == 0) {
                    if (result.length() > 0 && !result.substring(result.length() - 1).equals("零")) {
                        result.append("零");
                    }
                } else {
                    result.append(DIGITS[digit]).append(UNITS[unitIndex]);
                }

                if (unitIndex == 0 && bigUnitIndex != 0) {
                    result.append(BIG_UNITS[bigUnitIndex]);
                }
            }

            String res = result.toString().replace("零万", "万").replace("零亿", "亿").replaceAll("零+$", "");
            return res.isEmpty() ? "零" : res;
        }

        private static String convertDecimal(String decimalStr) {
            if (decimalStr.equals("00")) {
                return "整";
            }

            StringBuilder result = new StringBuilder();
            for (int i = 0; i < decimalStr.length(); i++) {
                int digit = decimalStr.charAt(i) - '0';
                if (i == 0 && digit != 0) {
                    result.append(DIGITS[digit]).append("角");
                } else if (i == 1 && digit != 0) {
                    result.append(DIGITS[digit]).append("分");
                }
            }

            return result.toString();
        }

        public static void main(String[] args) {
            double num = -100.00;
            String chineseAmount = numberToChinese(num);
            System.out.println(chineseAmount);  // 输出：负壹佰元整
        }
    }

